package com.kefu.call.controller;

import cn.hutool.core.util.StrUtil;
import com.kefu.call.http.client.CtiClient;

import com.kefu.call.http.model.*;
import com.kefu.common.context.KefuContext;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "呼叫接口")
@RestController
@RequestMapping("call/cti")
@Slf4j
public class CtiController {
    @Autowired
    CtiClient client;

    @PostMapping("/agentLogin")
    @ApiOperation(value = "坐席签入")
    public Result<String> agentLogin(@RequestBody CtiAgentLogin ctiLogin, HttpServletRequest request) {
        ctiLogin.setToken(getToken(request));
        ctiLogin.setAgentId(KefuContext.getUid());
        client.agentLogin(ctiLogin);
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/agentAvailable")
    @ApiOperation(value = "切换成空闲状态")
    public Result<String> agentAvailable(HttpServletRequest request) {
        client.agentAvailable(getToken(request));
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/agentOnBreak")
    @ApiOperation(value = "切换成小休状态")
    public Result<String> agentOnBreak(HttpServletRequest request,@RequestBody CtiAgentAux aux) {
    	aux.setToken(getToken(request));
        client.agentOnBreak(aux);
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/agentLogout")
    @ApiOperation(value = "坐席签出")
    public Result<String> logout(HttpServletRequest request) {
        client.agentLogout(getToken(request));
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/agentStatus")
    @ApiOperation(value = "获取当前坐席状态")
    public Result<CtiAgentStatus> agentStatus(HttpServletRequest request) {
        return  ResultGenerator.genSuccessResult(client.agentStatus(getToken(request), KefuContext.getUid()));
    }

    @PostMapping("/getEventMsg")
    @ApiOperation(value = "轮询事件消息")
    public Result<?> getEventMsg(@RequestBody CtiGetEventMsg param, HttpServletRequest request) {
        param.setToken(getToken(request));
        return ResultGenerator.genSuccessResult(client.getEventMsg(param));
    }

    @PostMapping("/getAgentMsg")
    @ApiOperation(value = "轮询事件消息",notes = "外部接口调用")
    public Result<List<CtiAgentEvent>> getAgentMsg(@RequestBody CtiGetEventMsg param, HttpServletRequest request) {
        param.setToken(getToken(request));
        return ResultGenerator.genSuccessResult(client.getAgentMsg(param));
    }

    @PostMapping("/makeCall")
    @ApiOperation(value = "拨打电话")
    public Result<CitMakeCallResult> makeCall(@RequestBody CitMakeCallParam param, HttpServletRequest request) {
        param.setToken(getToken(request));
        param.setAgentId(KefuContext.getUid());
        return ResultGenerator.genSuccessResult(client.makeCall(param));
    }

    @PostMapping("/hangUp")
    @ApiOperation(value = "结束当前通话")
    public Result<String> hangUp(@RequestBody(required = false)  CtiHangupVo hangupVo, HttpServletRequest request) {
        String token = getToken(request);
        if(hangupVo!=null && StrUtil.isNotBlank(hangupVo.getToken())){
            token = hangupVo.getToken();
        }
        log.info("cti_hangUp:{},{},{}",KefuContext.getKefuContext(),hangupVo,token);
        client.hangUp(token);
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/hold")
    @ApiOperation(value = "保持／取消保持")
    public Result<String> hold(HttpServletRequest request) {
        client.hold(getToken(request));
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/transfer")
    @ApiOperation(value = "转接")
    public Result<String> transfer(@RequestBody CitTransfer citTransfer, HttpServletRequest request) {
        citTransfer.setToken(getToken(request));
        client.transfer(citTransfer);
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/cancelXfer")
    @ApiOperation(value = "取消转接")
    public Result<String> cancelXfer(HttpServletRequest request) {
        String token = getToken(request);
        client.cancelXfer(token);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/satis")
    @ApiOperation(value = "满意度")
    public Result<String> satis(HttpServletRequest request) {
        client.satis(getToken(request));
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/eavesdrop")
    @ApiOperation(value = "监听")
    public Result<String>  eavesdrop(@RequestBody CtiEavesDrop param, HttpServletRequest request) {
        param.setToken(getToken(request));
        client.eavesdrop(param);
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/recvDtmf")
    @ApiOperation(value = "后操作按键")
    public Result<String> recvDtmf(@RequestBody CtiRecvDtmf param, HttpServletRequest request) {
        param.setToken(getToken(request));
        client.recvDtmf(param);
        return  ResultGenerator.genSuccessResult();
    }

    @PostMapping("/kickOut")
    @ApiOperation(value = "踢出坐席")
    public Result<String> kickOut(@RequestBody CtiKickOutParam ctiCurrency, HttpServletRequest request) {
        client.kickOut(ctiCurrency);
        return  ResultGenerator.genSuccessResult();

    }

    String getToken(HttpServletRequest request){
        String authorization = request.getHeader("Authorization").replace("Bearer","");
        return authorization.trim();
    }

}
